package com.example.chengzhengwei;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private TextView timeTextView;
    private Handler handler = new Handler();
    private Runnable runnable;
    private SerialApp serialApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        serialApp = (SerialApp) getApplication();

        timeTextView = findViewById(R.id.time);

        // 初始化按钮点击事件
        findViewById(R.id.buttonEnterTest).setOnClickListener(v -> {
            // 进入测试
            Toast.makeText(MainActivity.this, "进入测试", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, EnterTestActivity.class);
            startActivity(intent);
        });

        findViewById(R.id.buttonViewData).setOnClickListener(v -> {
            // 查看最近数据
            Toast.makeText(MainActivity.this, "查看最近数据", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, HistoryActivity.class);
            startActivity(intent);
        });

        findViewById(R.id.buttonSelfCheck).setOnClickListener(v -> {
            // 自检
            Toast.makeText(MainActivity.this, "自检", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, SelfCheckActivityV1.class);
            startActivity(intent);
        });

        findViewById(R.id.buttonSettings).setOnClickListener(v -> {
            // 设置
            Toast.makeText(MainActivity.this, "设置", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
            startActivity(intent);
        });

        // 更新时间
        updateTime();
        handler.postDelayed(runnable = new Runnable() {
            @Override
            public void run() {
                updateTime();
                handler.postDelayed(this, 1000);
            }
        }, 1000);

        // 开启串口连接
        initSerialConnection();
    }


    private void initSerialConnection() {
        // 设置串口状态监听器
        serialApp.setSerialStateListener(new SerialApp.SerialStateListener() {
            @Override
            public void onSerialConnected() {
                runOnUiThread(() ->
                        Toast.makeText(MainActivity.this, "串口连接成功", Toast.LENGTH_SHORT).show()
                );
            }

            @Override
            public void onSerialDisconnected() {
                runOnUiThread(() ->
                        Toast.makeText(MainActivity.this, "串口已断开", Toast.LENGTH_SHORT).show()
                );
            }

            @Override
            public void onSerialError(Exception e) {
                runOnUiThread(() ->
                        Toast.makeText(MainActivity.this, "串口错误: " + e.getMessage(), Toast.LENGTH_SHORT).show()
                );
            }
        });

        // 初始化串口连接
        serialApp.initSerialConnection();
    }

    private void updateTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d H:mm:ss", Locale.getDefault());
        String currentTime = sdf.format(new Date());
        timeTextView.setText(currentTime);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeCallbacks(runnable);
        // 释放串口资源
        serialApp.releaseSerialConnection();
    }
}